const commodtiyModel = require("../models/commodtiyModel.js");

module.exports = {
  async getCommodity(page,searchParams) {
    let {curPage,pageSize} = page;
    console.log("current+pageSize",curPage,pageSize)
    // 根据查询条件获取总数
    let total = await commodtiyModel.countDocuments(searchParams);
    // 根据查询条件获取具体数量的数据
    let data = await commodtiyModel.find(searchParams)
                    .limit(pageSize)
                    .skip((curPage - 1) * pageSize)
                    .sort({ "_id": -1 })
                    .exec();
    return {
      curPage,pageSize,total,rows:data,
    }
  },
  async deleteCommodityById(_id) {
    return await commodtiyModel.deleteOne({ _id }).exec();
  },
  async updateCommodityById(commodtiy) {
    // updateOne??findByIdAndUpdate
    return await commodtiyModel.updateOne({_id:commodtiy._id},commodtiy).exec()
  },
  async insertCommodity(obj) {
    return await commodtiyModel.create(obj);
  },
  async findById(_id) {
    return await commodtiyModel.findById(_id).exec();
  }
}